package component
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TouchEvent;
	import flash.ui.Multitouch;
	import flash.ui.MultitouchInputMode;
	
	import model.ControlVO;
	
	import mx.states.OverrideBase;
	
	import spark.components.Button;
	import spark.components.supportClasses.SkinnableComponent;
	
	public class ControlPad extends SkinnableComponent
	{
		[SkinPart]
		public var leftButton:Button;
		
		[SkinPart]
		public var upButton:Button;
		
		[SkinPart]
		public var rightButton:Button;
		
		[SkinPart]
		public var downButton:Button;
		
		[SkinPart]
		public var bButton:Button;
		
		public function ControlPad()
		{
			super();
			Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
		}
				
		private function touchEventHandler(event:TouchEvent):void
		{
			
			var controlVO:ControlVO = new ControlVO();
			
			if(event.type == TouchEvent.TOUCH_BEGIN)
			{
				switch(event.target)
				{
					case leftButton:
						controlVO.pressed = ControlVO.PRESS_LEFT;
						break
					case rightButton:
						controlVO.pressed = ControlVO.PRESS_RIGHT;
						break;
					case bButton:
						controlVO.pressed = ControlVO.PRESS_B;
						break;
				}
				Macario.controlSignal.dispatch(controlVO);
			}
			
			if(event.type == TouchEvent.TOUCH_END)
			{
				switch(event.target)
				{
					case leftButton:
						controlVO.pressed = ControlVO.RELEASE_LEFT;
						break
					case rightButton:
						controlVO.pressed = ControlVO.RELEASE_RIGHT;
						break;
					case bButton:
						controlVO.pressed = ControlVO.RELEASE_B;
						break;
				}
				Macario.controlSignal.dispatch(controlVO);
			}
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			if(instance == leftButton || instance == upButton || instance == rightButton || instance == downButton || instance == bButton)
			{
				instance.addEventListener(TouchEvent.TOUCH_BEGIN, touchEventHandler);
				instance.addEventListener(TouchEvent.TOUCH_END, touchEventHandler);
			}
		}
	}
}